#include "drv_speak.h"
// pb11 rx  pb10 tx		USART2
void drv_speak_init(void)
{
	GPIO_InitTypeDef  GPIO_InitStruct;
	USART_InitTypeDef  USART_InitStruct;
	NVIC_InitTypeDef  NVIC_InitStruct;
	
	RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA,  ENABLE);
	RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2,  ENABLE);

	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
	GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_3|GPIO_Pin_2;
	GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(  GPIOA,& GPIO_InitStruct);

	GPIO_PinAFConfig(  GPIOA,  GPIO_PinSource11,  GPIO_AF_USART2);
	GPIO_PinAFConfig(  GPIOA,  GPIO_PinSource10,  GPIO_AF_USART2);

	USART_InitStruct.USART_BaudRate=115200;
	USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
	USART_InitStruct.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
	USART_InitStruct.USART_Parity=USART_Parity_No;
	USART_InitStruct.USART_StopBits=USART_StopBits_1;
	USART_InitStruct.USART_WordLength=USART_WordLength_8b;
	USART_Init(USART2, &USART_InitStruct);

	USART_ITConfig( USART2,  USART_IT_RXNE,  ENABLE);
	USART_ITConfig( USART2,  USART_IT_IDLE,  ENABLE);	
	NVIC_InitStruct.NVIC_IRQChannel=USART2_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
	NVIC_Init(&NVIC_InitStruct);

	USART_ClearFlag(USART2,USART_FLAG_TC);
	USART_Cmd(USART2,ENABLE);

}

void drv_speak(void)
{ 
		if(ready_char == 1)
		{
				ready_char=0;
				zy_index=0;
			if(strcmp((const char*)recv,"open\r\n")==0)
			{
				drv_led_open();
				memset((void*)recv,0,6);
			}
			else if(strcmp((const char*)recv,"clos\r\n")==0)
			{
				drv_led_close();
				memset((void*)recv,0,6);
			}
			 if(strcmp((const char*)recv,"run\r\n")==0)
			{
				drv_dog_run();
				memset((void*)recv,0,6);
			}
			if(strcmp((const char*)recv,"stand\r\n")==0)
			{
				drv_dog_stand();
				memset((void*)recv,0,6);
			}
			else if(strcmp((const char*)recv,"lie\r\n")==0)
			{
				drv_dog_lie();
				memset((void*)recv,0,6);
			}
		
		}
		
}
